# -*- coding: utf-8 -*-

from flask import request, render_template
from loremipsum import generate_sentences
from random import randint

from ...forms import TagForm
from ..tag import _proj_t

def add_tag(lenta_id):
    if request.method == 'GET':
        return render_template('/lenta/tag/add_tag.html')
    else:
        form = TagForm(request.form)
        if form.is_valid():
            ctx = dict(namespace='ebanashka', lenta_id=100500)
            ctx.update(_proj_t(form.data))
            return render_template('/lenta/tag/add_tag_ok.html', tag=ctx)
        return render_template('/lenta/tag/add_tag.html', errors=form.errors, tag=form.data)


def view_tag(tag_name, lenta_id, namespace):
    tag = {
        'name': tag_name,
        'namespace': namespace,
        'lenta_id': lenta_id,
        'counter': 12
    }
    return render_template('/lenta/tag/view_tag.html', tag=tag)


def edit_tag(tag_name, lenta_id):
    if request.method == 'GET':
        tag = {
            'name': tag_name,
            'namespace': "ebanashka",
            'lenta_id': lenta_id,
            'counter': 12
        }
        return render_template('/lenta/tag/edit_tag.html', tag=TagForm(tag).htmlize())
    else:
        form = TagForm(request.form)
        if form.is_valid():
            ctx = dict(namespace='ebanashka', lenta_id=100500)
            ctx.update(_proj_t(form.data))
            return render_template('/lenta/tag/edit_tag_ok.html', tag=ctx)
        return render_template('/lenta/tag/edit_tag.html', errors=form.errors, tag=form.data)


def delete_tag(tag_name, lenta_id):
    if request.method == 'GET':
        tag = {
            'name': tag_name,
            'namespace': 'ebanashka',
            'lenta_id': lenta_id,
            'counter': 12
        }
        return render_template('/lenta/tag/delete_tag.html', tag=tag)
    else:
        return render_template('/lenta/tag/delete_tag_ok.html')


def view_all_tags_for_lenta(lenta_id, namespace):
    tags = ({'name': t[2][:randint(5,20)], 'counter': randint(5,100), 'namespace': namespace, 'lenta_id': 100500}
                for t in generate_sentences(30))
    return render_template('/lenta/tag/view_all_tags_for_lenta.html', tags=tags)
